<template>
  <van-sticky>
    <header class="home-header" :class="{ active: isActive }">
      <span class="category" @click="$router.push('/category')">
        <van-icon name="wap-nav" />
      </span>
      <div class="header-search">
        <span class="app-name">Mall商城</span>
        <span class="search-title" @click="$router.push('/product-list?from=home')"
          >山河无恙，人间皆安</span
        >
      </div>
      <van-icon name="manager" @click="$router.push('/user')" v-if="alreadyLogin"/>
      <span class="user" @click="$router.push('/login')" v-else>登录</span>
    </header>
  </van-sticky>
</template>
<script setup>
import { onMounted } from 'vue'
import {getLocal} from '@/utils';
const isActive = ref(false)
const setIsActive = () => {
  let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
  //如果向上卷去的高度大于100像素的话就把isActive设置为true
  isActive.value = scrollTop > 50
}
onMounted(() => {
  window.addEventListener('scroll', setIsActive)
})
onUnmounted(() => {
  window.removeEventListener('scroll', setIsActive)
})
const alreadyLogin = ref(!!getLocal('token'))
</script>
<style lang="less">
.home-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  left: 0;
  top: 0;
  width: 100%;
  height: 50px;
  line-height: 50px;
  padding: 0 15px;
  box-sizing: border-box;
  font-size: 15px;
  color: @primary-color;
  z-index: 1000;
  &.active {
    background-color: @primary-color;
    .category,
    .user {
      color: #fff;
    }
  }
  .category {
    font-size: 20px;
    font-weight: bold;
  }
  .header-search {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    height: 30px;
    line-height: 30px;
    width: 75%;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 20px;
    .app-name {
      font-size: 20px;
      font-weight: bold;
    }
    .search-title {
      color: #666;
    }
  }
}
</style>
